package com.aerospace.rpc1;

import com.aerospace.User;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * @Author: Aerospace
 * @Date: 2021/5/12 18:26
 * @Description: Dream begins
 *
 * 这个例子使用的是最原始的客户端服务端交互方式，硬编码完成功能，非常紧耦合
 */
public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",8099);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeInt(123); //注意点，这里是写死的，缺乏灵活性
        //发送出要查询的id
        socket.getOutputStream().write(baos.toByteArray());
        socket.getOutputStream().flush();
        //接收服务端返回的结果
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        int id = dis.readInt();
        String name = dis.readUTF();
        User user = new User(id, name);
        System.out.println(user);
        dos.close();
        socket.close();
    }
}
